/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Comp_08_Toolbar.java]
 * Creation Date        [23-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_05_swing.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;

/**
 * Swing浮动工具栏
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Comp_08_Toolbar {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ToolbarFrame frame = new ToolbarFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

/**
 * Toolbar窗体
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class ToolbarFrame extends JFrame {

	private static final long serialVersionUID = 3530257716304529705L;

	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;

	private JPanel panel = null;

	/**
	 * 构造方法
	 */
	public ToolbarFrame() {
		setTitle("Toolbar Test");
		this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setLocationByPlatform(true);
		setResizable(false);

		panel = new JPanel();
		this.add(panel, BorderLayout.CENTER);

		Action redAction = new ColorAction("Red", Color.RED);
		Action blueAction = new ColorAction("Blue", Color.BLUE);
		Action yellowAction = new ColorAction("Yellow", Color.YELLOW);

		Action exitAction = new AbstractAction() {
			private static final long serialVersionUID = -2550476356935942753L;

			@Override
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		};
		exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit");
		exitAction.putValue(Action.NAME, "Exit");

		JToolBar toolBar = new JToolBar();
		toolBar.add(blueAction);
		toolBar.add(redAction);
		toolBar.add(yellowAction);
		toolBar.add(exitAction);
		this.add(toolBar, BorderLayout.NORTH);

		JMenu menu = new JMenu("Color");
		menu.add(blueAction);
		menu.add(redAction);
		menu.add(yellowAction);
		menu.add(exitAction);

		JMenuBar menuBar = new JMenuBar();
		menuBar.add(menu);
		setJMenuBar(menuBar);

	}

	/**
	 * @author 不落的太阳(Sean Yang)
	 * @version 1.0
	 * @since JDK 6
	 */
	private class ColorAction extends AbstractAction {

		private static final long serialVersionUID = 3193800863814092691L;

		public ColorAction(String name, Color color) {
			putValue(Action.NAME, name);
			putValue(Action.SHORT_DESCRIPTION, name + " background");// 添加提示信息
			putValue("Color", color);
		}

		@Override
		public void actionPerformed(ActionEvent event) {
			Color color = (Color) getValue("Color");
			panel.setBackground(color);
		}

	}

}
